<?php 
class categoryController extends accessController{
    public static function _right(){
        return array(
            array('分类管理',1),
            array('分类添加','add,add_'),
            array('分类列表','mylist'),
            array('分类编辑','mod,mod_'),
            array('分类删除','del'),
            array('分类数据修复','repair_me'),
            array('分类缓存生成','cache_me'),
        );
    }
    /*
    * 分类修复
    */
    public function repair_me(){
        $mid=intval($_GET['mid']);
		if($mid==0){
			exit('mid error!');
		}
        $category_service=load_service('category');
		$category_service->define_table($mid);
        $r=$category_service->repair_path();
        $r2=$category_service->repair_childs();
        if($r && $r2){
            alert('分类修复成功！');
            back();
        }else{
            alert('分类修复失败！');
            back();
        }
    }
    /*
    * 分类缓存更新
    */
    public function cache_me(){
        $mid=intval($_GET['mid']);
		if($mid==0){
			exit('mid error!');
		}
        $category_service=load_service('category');
		$category_service->define_table($mid);
        $r=$category_service->cache_me();
        if($r){
            alert('分类缓存成功！');
            back();
        }else{
            alert('分类缓存失败！');
            back();
        }
    }
    /**
     * 添加分类
     */
    public function add(){
		$mid=intval($_GET['mid']);
		if($mid==0){
			exit('mid error!');
		}
        $selected_c=intval($_GET['selected_c']);
        $category_service=load_service('category');
		$category_service->define_table($mid);
        $category_arr=$category_service->with_option($mid);
        $s=time();
        include($this->viewer_dir.'category/add.php');
    }
	/*
	* 添加分类提交
	*/
    public function add_(){
        if($_POST){
			$category_service=load_service('category');
			$category_service->define_table($_POST['mid']);
            $r=$category_service->add_();
            alert($r['message']);
            back();
        }
    }
	/*
	* 分类修改
	*/
	public function mod(){
		$id=intval($_GET['id']);
		$mid=intval($_GET['mid']);
		if($mid==0){
			exit('mid error!');
		}
        $category_service=load_service('category');
		$category_service->define_table($mid);
        $category_arr=$category_service->with_option($mid);
		
        $data=$category_service->mod($_GET['id']);
		
		$radio_disabled=$category_service->radio_disabled($id);
        if($data){
            include($this->viewer_dir.'category/mod.php');
        }
	}
	/*
	* 分类修改提交
	*/
	public function mod_(){
        $category_service=load_service('category');
		$category_service->define_table($_POST['mid']);
        $id=intval($_GET['id']);
		$r=$category_service->check_category_for_mod_($id,$_POST['old_parent_id'],$_POST['parent_id']);
		if(!$r){
			alert('上级分类选择不正确！');
			back();
		}
        $r=$category_service->mod_($_POST,$id);
        if($r[0]){
            alert('分类编辑成功！');
            reload();
        }else{
            alert('分类编辑失败！'.$r[1]);
            back();
        }
	}
    /**
     * 分类列表
     */
    public function mylist(){
		$mid=intval($_GET['mid']);
		if($mid==0){
			exit('mid error!');
		}
        $category_service=load_service('category');
		$category_service->define_table($mid);
        $category_arr=$category_service->with_option($mid);
        include($this->viewer_dir.'category/mylist.php');
    }
	//删除
	public function del(){
		$id=intval($_GET['id']);
		$mid=intval($_GET['mid']);
		$category_service=load_service('category');
		$category_service->define_table($mid);
		$r=$category_service->del($id);
		autoalert($r[0],$r[1]);
		back();
	}
//
}
